<?php

class AcctSubjectTable extends Doctrine_Table {
    public function getAcctSubjectes($acct_set_id) {

        //取所属会计账套的会计科目
        //$q = Doctrine_Query::create()->addFrom("AcctSubject")
        $q = $this->createQuery()
                ->where("AcctSubject.acct_set_id = ?", $acct_set_id)
                ->orderBy("AcctSubject.subject_id");

        return $q->execute();
    }

    public function getAcctSubjectesAssoc($subjectes) {
        $subjectes_arr = array();

        foreach($subjectes as $subject) {
            $subjectes_arr[$subject->getSubjectId()] = $subject->getSubjectName();
        }

        return $subjectes_arr;
    }

    //将根据会计科目生成科目树
    /*Demo:
     *  1   //科目类别 资产/负债/权益/共同/损益/成本
     *   一级科目
     *   一级科目
     *    二级科目
     *     三级科目
     *    二级科目
     *     三级科目
     * 
    */
    public function getAcctSubjectesTree($subjectes) {
        $subject_tree = array();
        foreach($subjectes as $subject) {
            //echo $subject->getSubjectId() . $subject->getSubjectName()."\r\n";
            //echo $subject->getSubjectName();
            $levels = $this->splitSubjectId($subject->getSubjectId());
            //print_r($levels);
            if(!empty($levels[0])) {
                if( ! isset($subject_tree[$subject->getCategoryId()][$levels[0]])) {
                    $subject_tree[$subject->getCategoryId()][$levels[0]] = "";
                }
            }

            if(!empty($levels[1])) {
                if(! isset($subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]])) {
                    $subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]] = "";
                }
            }

            if(!empty($levels[2])) {
                //echo "levels 3 不为空" . $subject->getCategoryId() . "-" . $levels[0].$levels[1].$levels[2] . $subject->getSubjectName(). "\r\n";
                if(! isset($subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]][$levels[0].$levels[1].$levels[2]])) {
                    $subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]][$levels[0].$levels[1].$levels[2]] = "";
                }

            }

            if(!empty($levels[3])) {

                if(! isset($subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]][$levels[0].$levels[1].$levels[2]][$levels[0].$levels[1].$levels[2].$levels[3]])) {
                    $subject_tree[$subject->getCategoryId()][$levels[0]][$levels[0].$levels[1]][$levels[0].$levels[1].$levels[2]][$levels[0].$levels[1].$levels[2].$levels[3]] = "";
                }

            }

        }

        return $subject_tree;
    }

    //根据树生成select的options选单
    public function generateAcctSubjectOptions($subject_tree, $subjectes) {
        //取会计科目类别
        $categories_arr = Doctrine::getTable("AcctCategory")->getAcctCategoriesAssoc();
        //print_r($categories_arr);
        $select_str = "";
        foreach($subject_tree as $category_id => $subtree) {
            //$select_str .= '<optgroup label="' . $categories_arr[$category_id].'">\r\n';
            //$select_str .= $this->convertSubjectesTree2Option($subtree, $subjectes);
            //$select_str .= "</optgroup>\r\n";
            $select_str .= '<optgroup label="' . $categories_arr[$category_id].'">' . $this->convertSubjectesTree2Option($subtree, $subjectes) . "</optgroup>\r\n";
        }

        return $select_str;

    }


    private function convertSubjectesTree2Option($tree, $subjectes) {
        //static $arr = "";
        //html4的option不支持多层级联
        $arr = "";
        foreach($tree as $acct_subject => $content) {
            //echo $acct_subject." ". $subjectes[$acct_subject]."\r\n";
            if(is_array($content)) {
                //echo "<optgroup label='".$acct_subject." ". $subjectes[$acct_subject]."'>" .  $this->convertSubjectesTree2Option($content, $subjectes) . "</optgroup>\r\n";
                //echo "<option value='".$acct_subject."'>".$subjectes[$acct_subject]."</option>\r\n";
                $arr .=  $this->convertSubjectesTree2Option($content, $subjectes);
            } else {
                $arr .= '<option value="'.$acct_subject.'">&nbsp;&nbsp;'.$acct_subject . "-" . $subjectes[$acct_subject].'</option>';
            }
        }
        return $arr;
    }
    

    //将会计科目编号进行分解,根据4-3-2-2原则
    private function splitSubjectId($subject_id) {
        $lv1 = substr($subject_id,0,4);
        $lv2 = substr($subject_id,4,3);
        $lv3 = substr($subject_id,7,2);
        $lv4 = substr($subject_id,9,2);

        return array($lv1,$lv2,$lv3,$lv4);

    }


    //get unique id of specific subject
    public function getUniqueSubjectId(AcctSet $acct_set,$subject_id) {
        //$q = Doctrine_Query::create()->from("AcctSubject")
        $q = $this->createQuery()
                ->where("acct_set_id = ?", $acct_set->getId())
                ->andWhere("subject_id = ?", $subject_id);
        
        return $q->fetchOne()->getId();
        
    }


}
